import os
import logging
import datetime
from docwork.config import BASE_PATH


class Message:
    """
    邮件消息体构造
    """
    def __init__(self, rawmaillog_id, sellside_docid=0, subject='', from_='', to='', cc='', bodyhtml='', bodytext=''):
        self.rawmaillog_id = rawmaillog_id
        self.sellside_docid = sellside_docid
        self.subject = subject.strip()
        self.from_ = from_.strip()
        self.from_real_name = ''
        self.to_ = to.strip()
        self.to_real_name = ''
        self.cc = cc.strip()
        self.cc_real_name = ''
        self.message_id = ''
        self.body_html = bodyhtml.strip()
        self.body_text = bodytext.strip()
        self.content = ''
        self.receive_time = datetime.datetime.now()
        self.attachments = []
        self.uid = ''
        self.analyse_success = False
        self.customer_id = 1

    def add_attachment(self, attachment_name, path, content_id=''):
        attach_path = BASE_PATH.joinpath(path)
        if attach_path.exists():
            attachment = dict()
            attachment['attachName'] = attachment_name.strip()
            attachment['path'] = str(attach_path)
            attachment['attachSize'] = os.path.getsize(attach_path)
            attachment['Content-ID'] = content_id[1:len(content_id.strip()) - 1]
            logging.info(f"Added attachment:{attachment}")
            self.attachments.append(attachment)
        else:
            logging.warning(f"Attachment path:{path} not exists or is not a file, not add to attachment list")

